import { store } from '@/stores';
import { Locale } from 'vant';
import zhCNv from 'vant/es/locale/lang/zh-CN';
import enUSv from 'vant/es/locale/lang/en-US';

const mappingLocale = {
    'zh-CN': zhCNv,
    'en-US': enUSv,
};

export type Typelanguage = 'en-US' | 'zh-CN';

// setup
export const useAppStore = defineStore('app', () => {
    // language
    const language = useStorage('language', 'zh-CN');

    const changeLanguage = (lang: string) => {
        language.value = lang;
        Locale.use(lang, mappingLocale[lang as Typelanguage]);
    };

    return {
        language,
        changeLanguage,
    };
});

// 手动提供给 useStore() 函数 pinia 实例
// https://pinia.vuejs.org/zh/core-concepts/outside-component-usage.html#using-a-store-outside-of-a-component
export function useAppStoreHook() {
    return useAppStore(store);
}
